/*
 * This source code is part of TWaver 4.0
 *
 * Serva Software PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 * Copyright 2002 - 2011 Serva Software. All rights reserved.
 */

package demo.chart.bubble;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JCheckBox;
import javax.swing.JSlider;
import javax.swing.JToolBar;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import twaver.Element;
import twaver.Node;
import twaver.TDataBox;
import twaver.TWaverConst;
import twaver.chart.Bubble;
import twaver.chart.BubbleChart;
import demo.chart.Portlet;

public class QuadrantChart extends Portlet {
	private TDataBox box = new TDataBox();
	private BubbleChart chart = new BubbleChart(box);

	public QuadrantChart() {
		super.initialize(chart);
		initElements();
		chart.setShadowOffset(0);
		chart.setXGap(20);
		chart.setYGap(20);
		chart.setYScaleMaxValue(50);
		chart.setYScaleMinValue(-50);
		chart.setXScaleMinValue(-50);
		chart.setXScaleMaxValue(50);
		chart.setXAxisStartPosition(0);
		chart.setYAxisStartPosition(0);
		chart.setYScaleTextVisible(true);
		chart.setYScaleValueGap(10);
		chart.setBubbleGradientFactory(TWaverConst.GRADIENT_LINE_N);
		chart.setShapeLineVisible(true);
		chart.setBackgroundVisible(true);
		chart.getLegendPane().setVisible(false);
		chart.setValueTextCenter(true);
		List list = new ArrayList();
		list.add(new Integer(5));
		chart.setXScaleHideValueIndex(list);
		chart.setYScaleHideValueIndex(list);
	}

	public void initElements() {
		initElement("1", Color.RED, TWaverConst.INFLEXION_STYLE_CIRCLE, 25, 25, 30);
		initElement("11", Color.RED.brighter(), TWaverConst.INFLEXION_STYLE_CIRCLE, 10, 25, 10);
		initElement("12", Color.RED.darker(), TWaverConst.INFLEXION_STYLE_CIRCLE, 40, 10, 20);

		initElement("2", Color.GREEN, TWaverConst.INFLEXION_STYLE_DIAMOND, -25, 25, 30);
		initElement("21", Color.GREEN.brighter(), TWaverConst.INFLEXION_STYLE_DIAMOND, -10, 10, 10);
		initElement("22", Color.GREEN.darker(), TWaverConst.INFLEXION_STYLE_DIAMOND, -40, 20, 20);

		initElement("3", Color.BLUE, TWaverConst.INFLEXION_STYLE_RECTANGLE, -25, -25, 30);
		initElement("31", Color.BLUE.brighter(), TWaverConst.INFLEXION_STYLE_RECTANGLE, -10, -20, 10);
		initElement("32", Color.BLUE.darker(), TWaverConst.INFLEXION_STYLE_RECTANGLE, -40, -10, 5);

		initElement("4", Color.ORANGE, TWaverConst.INFLEXION_STYLE_TRIANGLE, 20, -25, 20);
		initElement("41", Color.ORANGE.darker(), TWaverConst.INFLEXION_STYLE_TRIANGLE, 10, -40, 10);
		initElement("42", Color.ORANGE.brighter(), TWaverConst.INFLEXION_STYLE_TRIANGLE, 35, -15, 30);
	}

	public void initElement(String text, Color color, int style, double x, double y, double z) {
		Element element = new Node();
		element.setName(text);
		element.putChartBubbleStyle(style);
		element.putChartColor(color);
		Bubble bubble = new Bubble(x, y, z);
		element.addChartBubble(bubble);
		box.addElement(element);
	}

	public JToolBar getControlPanel() {
		JToolBar toolbar = super.getControlPanel();
		final JCheckBox xCheckBox = new JCheckBox("x");
		final JCheckBox yCheckBox = new JCheckBox("y");
		xCheckBox.setPreferredSize(new Dimension(xCheckBox.getPreferredSize().width + 5, 20));
		yCheckBox.setPreferredSize(new Dimension(yCheckBox.getPreferredSize().width + 5, 20));
		xCheckBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (!xCheckBox.isSelected()) {
					List list = new ArrayList();
					list.add(new Integer(5));
					chart.setXScaleHideValueIndex(list);
				} else {
					chart.setXScaleHideValueIndex(null);
				}
			}
		});
		yCheckBox.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				if (!yCheckBox.isSelected()) {
					List list = new ArrayList();
					list.add(new Integer(5));
					chart.setYScaleHideValueIndex(list);
				} else {
					chart.setYScaleHideValueIndex(null);
				}
			}
		});
		toolbar.add(xCheckBox);
		toolbar.add(yCheckBox);

		final JSlider slider = new JSlider(-50, 50);
		slider.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				chart.setYAxisStartPosition(slider.getValue());
			}
		});
		toolbar.add(slider);
		return toolbar;
	}
}